package com.hopes.bizz.poll.statistic.service.support;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hopes.bizz.common.core.util.CollectionUtils;
import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.poll.analy.dto.ReportDTO;
import com.hopes.bizz.poll.analy.vo.ReportVO;
import com.hopes.bizz.poll.monitor.entity.PollMonitorFactorEntity;
import com.hopes.bizz.poll.monitor.service.PollMonitorFactorService;
import com.hopes.bizz.poll.statistic.service.DataReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author yumr
 * @ProjectName bizz
 * @Description 报表导出支撑类
 * @time 2023/6/8 22:57
 */
@Component
@RequiredArgsConstructor
public class ReportExportSupport {

    private final DataReportService dataReportService;

    private final PollMonitorFactorService pollMonitorFactorService;

    /**
     * 根据站点Id获取表头数据
     * @param siteId  站点信息
     * @return        表头数据
     */
    public List<List<String>> reportHead(Long siteId) {
        final List<PollMonitorFactorEntity> listMonitorFactor = this.listMonitorFactor(siteId);
        if (CollectionUtils.isEmpty(listMonitorFactor)) {
            return new ArrayList<>();
        }
        return this.handlerToHead(listMonitorFactor);
    }

    /**
     * 站点因子列表转表头数据
     * @param listMonitorFactor 站点因子列表
     * @return                  表头数据
     */
    private List<List<String>> handlerToHead(List<PollMonitorFactorEntity> listMonitorFactor) {
        final List<List<String>> result = new ArrayList<>();

        final List<String> timeList = new ArrayList<>();
        timeList.add("时间");
        result.add(timeList);

        for (PollMonitorFactorEntity monitorFactor : listMonitorFactor) {
            boolean hasHead = false;
            if (StringUtils.isNotBlank(monitorFactor.getUnit())) {
                final List<String> avg = new ArrayList<>();
                avg.add(monitorFactor.getFactorIdStr());
                avg.add(monitorFactor.getUnit());
                hasHead = true;
                result.add(avg);
            }
//            if (monitorFactor.getZsFlag()) {
//                final List<String> zs = new ArrayList<>();
//                zs.add(monitorFactor.getFactorIdStr());
//                zs.add("折算\n" + monitorFactor.getUnit());
//                hasHead = true;
//                result.add(zs);
//            }
            if (StringUtils.isNotBlank(monitorFactor.getStatisticUnit())) {
                final List<String> cou = new ArrayList<>();
                cou.add(monitorFactor.getFactorIdStr());
                cou.add(monitorFactor.getStatisticUnit());
                hasHead = true;
                result.add(cou);
            }

            if (!hasHead) {
                final List<String> rtd = new ArrayList<>();
                rtd.add(monitorFactor.getFactorIdStr());
                result.add(rtd);
            }
        }

        final List<String> remark = new ArrayList<>();
        remark.add("备注");
        result.add(remark);

        return result;
    }

    /**
     * 查询站点因子信息
     * @param siteId 站点Id
     * @return       站点因子列表
     */
    private List<PollMonitorFactorEntity> listMonitorFactor(Long siteId) {
        if (Objects.isNull(siteId)) {
            return new ArrayList<>();
        }
        final LambdaQueryWrapper<PollMonitorFactorEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PollMonitorFactorEntity::getMonitorSiteId, siteId);
        return pollMonitorFactorService.list(queryWrapper);
    }

    /**
     * 报表数据
     * @param reportDTO  查询对象
     * @return           结果集
     */
    public Collection<?> reportDataList(ReportDTO reportDTO) {
        final List<ReportVO> reportVOList = dataReportService.reportAnalyze(reportDTO);
        if (CollectionUtils.isEmpty(reportVOList)) {
            return new ArrayList<>();
        }
        final List<PollMonitorFactorEntity> listMonitorFactor = this.listMonitorFactor(reportDTO.getMonitorSiteId());
        if (CollectionUtils.isEmpty(listMonitorFactor)) {
            return new ArrayList<>();
        }
        return handlerToData(listMonitorFactor, reportVOList);
    }

    private Collection<?> handlerToData(List<PollMonitorFactorEntity> listMonitorFactor, List<ReportVO> reportVOList) {
        final List<List<String>> result = new ArrayList<>();

        for (ReportVO reportVO : reportVOList) {
            final List<String> item = new ArrayList<>();
            item.add(reportVO.getTitle());

            for (PollMonitorFactorEntity monitorFactor : listMonitorFactor) {
                boolean hasData = false;
                final JSONObject data = reportVO.getData().getJSONObject(monitorFactor.getFactorCode());
                if (StringUtils.isNotBlank(monitorFactor.getUnit())) {
                    item.add(MapUtil.getStr(data, "avg", "0"));
                    hasData = true;
                }
//                if (monitorFactor.getZsFlag()) {
//                    item.add(MapUtil.getStr(data, "zsVal", "0"));
//                    hasData = true;
//                }
                if (StringUtils.isNotBlank(monitorFactor.getStatisticUnit())) {
                    item.add(MapUtil.getStr(data, "cou", "0"));
                    hasData = true;
                }
                if (!hasData) {
                    item.add(MapUtil.getStr(data, "val", "0"));
                }
            }
            result.add(item);
        }
        return result;
    }
}
